import pdfplumber
import sys

def pdf2txt(pdf_path):
    txt = ''
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            txt = txt + page.extract_text()
    return txt

def main():
    if len(sys.argv) != 3:
        print("Usage: python script.py <path_to_pdf> <path_to_text>")
        sys.exit(1)

    pdf_file_path = sys.argv[1]
    content_file_path = sys.argv[2]

    pdfContent = pdf2txt(pdf_file_path) 
    # Remove unnecessary characters like newlines
    cleaned_content = pdfContent.replace('\r\n', '\n')
    cleaned_content = cleaned_content.replace(' ', '')
    # Split the text into lines based on Chinese period (句号)
    lines = cleaned_content.split('。')

    # Filter out empty lines
    lines = [line.strip() for line in lines if line.strip()]

    # Write the cleaned and split text to a new file
    with open(content_file_path, 'w', encoding='utf-8') as output_file:
        for line in lines:
            output_file.write(line + '。\n')   

if __name__ == "__main__":
    main()